package com.sixmac.platform.controller;

import com.sixmac.common.base.BaseController;
import com.sixmac.core.annotation.Validator;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.shiro.ShiroKit;
import com.sixmac.core.toolbox.Paras;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.JsonKit;
import com.sixmac.module.AdminMenu;
import com.sixmac.module.AdminUser;
import com.sixmac.police.service.AdminMenuService;
import com.sixmac.police.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * AdminUserController
 * 2018-09-26 11:15:36
 * @author Blade
 */
@Controller
@RequestMapping("/adminUser")
public class AdminUserController extends BaseController {
	private static String CODE = "adminUser";
	private static String PREFIX = "t_admin_user";
	private static String LIST_SOURCE = "adminUser.list";
	private static String BASE_PATH = "/platform/adminUser/";
	
	@Autowired
	AdminUserService service;
	
	@RequestMapping(KEY_MAIN)
	public String index(ModelMap mm) {
		mm.put("code", CODE);
		return BASE_PATH + "adminUser.html";
	}

	@RequestMapping(KEY_ADD)
	public String add(ModelMap mm) {
		mm.put("code", CODE);
		return BASE_PATH + "adminUser_add.html";
	}

	@RequestMapping(KEY_EDIT + "/{id}")
	public String edit(@PathVariable Integer id, ModelMap mm) {
		AdminUser adminUser = service.findById(id);
		mm.put("model", JsonKit.toJson(adminUser));
		mm.put("id", id);
		mm.put("code", CODE);
		return BASE_PATH + "adminUser_edit.html";
	}

	@RequestMapping(KEY_VIEW + "/{id}")
	public String view(@PathVariable Integer id, ModelMap mm) {
		AdminUser adminUser = service.findById(id);
		mm.put("model", JsonKit.toJson(adminUser));
		mm.put("id", id);
		mm.put("code", CODE);
		return BASE_PATH + "adminUser_view.html";
	}

	@ResponseBody
	@RequestMapping(KEY_LIST)
	public Object list() {
		Object grid = paginate(LIST_SOURCE);
		return grid;
	}

	@ResponseBody
	@RequestMapping(KEY_SAVE)
	public AjaxResult save() {
		String number = getParameter("t_admin_user.number");
		String password = getParameter("t_admin_user.password");
		String name = getParameter("t_admin_user.name");
		String phone = getParameter("t_admin_user.phone");
		String areas = getParameter("t_admin_user.area");
		String citys = getParameter("t_admin_user.city");

		if (citys==null){
			return error("请选择城市");
		}

		if (citys.equals("请选择")){
			return error("请选择城市");
		}

		Integer area = 0;
		if (!areas.equals("请选择")){
			area = Integer.valueOf(areas);
		}

		AdminUser user = Blade.create(AdminUser.class).findFirstBy("number = #{number}", Paras.create().set("number", number));

		if (user!=null){
			return error("注册失败，用户已存在");
		}

		AdminUser adminUser = new AdminUser();
		adminUser.setNumber(number);
		adminUser.setpId(0);
		adminUser.setName(name);
		adminUser.setPassword(ShiroKit.md5(password, ""));
		adminUser.setRoleId(0);
		adminUser.setCreateDate(new Date());
		adminUser.setPhone(phone);
		if (area!=0){
			adminUser.setAreaId(area);
		}else if (citys!=null){
			adminUser.setCityId(Integer.parseInt(citys));
		}
		boolean temp = service.save(adminUser);
		if (temp) {
			return success(SAVE_SUCCESS_MSG);
		} else {
			return error(SAVE_FAIL_MSG);
		}
	}

	@ResponseBody
	@RequestMapping(KEY_UPDATE)
	public AjaxResult update() {
		String number = getParameter("t_admin_user.number");
		String password = getParameter("t_admin_user.password");
		String phone = getParameter("t_admin_user.phone");
		Integer area = getParameterToInt("t_admin_user.area",0);
		AdminUser adminUser = mapping(PREFIX, AdminUser.class);
		adminUser.setNumber(number);
		adminUser.setPassword(ShiroKit.md5(password, ""));
		adminUser.setPhone(phone);
		adminUser.setAreaId(area);
		boolean temp = service.update(adminUser);
		if (temp) {
			return success(UPDATE_SUCCESS_MSG);
		} else {
			return error(UPDATE_FAIL_MSG);
		}
	}

	@ResponseBody
	@RequestMapping(KEY_REMOVE)
	public AjaxResult remove(@RequestParam String ids) {
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			return success(DEL_SUCCESS_MSG);
		} else {
			return error(DEL_FAIL_MSG);
		}
	}
}
